/*
 * PopupGuiCore.hpp
 *
 *  Created on: Jun 7, 2012
 *      Author: guillou
 */

#ifndef POPUPGUICORE_HPP_
#define POPUPGUICORE_HPP_

#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>

#include <PopupGui.hpp>
#include <PopupClientUI.hpp>
#include <PopupSettings.hpp>
#include <PopupClientThread.hpp>
#include <PopupImageViewer.hpp>
#include <PopupMessageDialog.hpp>
#include <PopupLoginDialog.hpp>
#include <PopupSetupDialog.hpp>
#include <PopupLogsDialog.hpp>
#include <PopupTransfersDialog.hpp>
#include <PopupUsersManager.hpp>
#include <PopupUpdateManager.hpp>

class PopupGuiCore : public QObject, public PopupUsersObserver
{
  Q_OBJECT

public:
  static PopupGuiCore* instance();
  virtual ~PopupGuiCore() {}

  void saveSession();
  void exit();
  void localUserUpdated(unsigned short p_mask);

  enum { POPUP_USER_ID = Qt::UserRole+1 };

  //------------------------------------------
  // Slots
  //------------------------------------------

public slots:
  void slot_showBalloon(const QString & p_header,
                       const QString & p_text);
  void slot_onConnectionUpdate(bool p_connected);
  void slot_updateSystrayIcon();
  void slot_onSettingsUpdated();
  void slot_showError(const QString & p_error);
  void slot_onUpdateThreadFinished();
  void slot_onUpdateAvailable(float p_version, const QString & p_changes);
  void slot_onUpdateFailed();
  void slot_showDialog(PopupGui::DialogID p_id);

  //------------------------------------------
  // Signals
  //------------------------------------------

signals:
  void signal_showBalloon(const QString & p_header,
                         const QString & p_text);
  void signal_setConnectionParams(const QString &, const QString, const QString &, int, bool);
  void signal_userModeUpdated();
  void signal_killUpdateManagerThread();
  void signal_launchUpdate();
  void signal_disconnectClient();
  void signal_restoreThread(const PopupGui::PopupThreadSave &);

  //------------------------------------------
  // Private stuff
  //------------------------------------------

  //========= Slots

private slots:
  void slot_iconActivated(QSystemTrayIcon::ActivationReason p_reason);

  //========= Methods
private:
  PopupGuiCore();
  void restore();
  void initSystray();
  void connectCoreAndSystray();
  void connectCoreAndClientThread();
  void connectCoreAndSetupDialog();
  void connectUsersManagerAndClientThread();
  void connectMessageDialogAndCore();
  void connectMessageDialogAndClientThread();
  void connectMessageDialogAndSetupDialog();
  void connectMessageDialogAndImageViewer();
  void connectLoginDialogAndClientThread();
  void connectSetupDialogAndClientThread();
  void connectCoreAndUpdateManagerThread();

  //========= Member variables

  static PopupGuiCore *m_instance;

  //..... Logging and settings
  PopupSettings*        m_settings;
  //..... GUI artefacts
  PopupLoginDialog      m_loginDlg;
  PopupLogsDialog       m_logsDlg;
  PopupSetupDialog      m_setupDlg;
  PopupMessageDialog    m_messageDlg;
  PopupImageViewer      m_imageviewer;
  QSystemTrayIcon*      m_sticon;
  PopupTransfersDialog  m_transfersDlg;
  //..... Client thread
  PopupClientThread*    m_clientThread;
  bool                  m_connected;
  //..... Client list management
  PopupUsersManager*    m_usersManager;
  //..... Update management
  PopupUpdateManager*   m_updateManager;
};

#endif /* POPUPGUICORE_HPP_ */
